<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <!-- 头部布局 -->
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <!-- 负责人 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树形组件布局 -->
        <el-tree :data="list" :props="defaultProps" :default-expand-all="true">
          <!-- 自定义 - 卡槽 -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <!-- 负责人 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 操作栏 -->
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="add(data)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="edit(data)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="del(data)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 添加的弹层组件 -->
    <add-dept
      ref="addDep"
      :show-dialog="showDialog"
      :cur-node="curNode"
      :all-list="allList"
      @update-dep="hGetDepartments"
      @close-dialog="delDia"
    />
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { transTreeData } from '@/utils/transTreeData'
import AddDept from './components/add-dept.vue'
export default {
  components: {
    AddDept
  },
  data() {
    return {
      list: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      showDialog: false,
      curNode: null,
      allList: []
    }
  },
  created() {
    this.hGetDepartments()
  },
  methods: {
    // 获取组织架构数据
    async hGetDepartments() {
      const res = await getDepartments()
      // console.log(res)
      // 拿到code码 数组
      this.allList = res.depts.map(item => item.code)
      // console.log(this.allList)
      // 接口返回数据 传入树形数据处理函数 得到属性数据赋给list
      this.list = transTreeData(res.depts)
    },
    // 删除部门
    del(data) {
      // 1.询问是否删除
      this.$confirm('确定删除么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 2.调接口
        await delDepartments(data.id)
        // 3.更新数据
        this.hGetDepartments()
        // 4.提示删除完毕
        this.$message.success('删除成功!')
      }).catch((error) => {
        console.log(error)
      })
    },
    // 添加部门
    add(data) {
      // console.log('添加')
      this.showDialog = true
      this.curNode = data
    },
    // 父组件内 子传父的 自定义事件 的方法
    delDia() {
      this.showDialog = false
    },
    // 编辑部门
    edit(data) {
      this.curNode = data
      this.showDialog = true
      this.$refs.addDep.hGetDepartDetail(data.id)
    }
  }
}
</script>

<style>

</style>
